{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "import datasets\n",
    "from datasets import Dataset\n",
    "import json"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4024\n"
     ]
    }
   ],
   "source": [
    "with open(\"cases_pool.json\") as fin:\n",
    "    data_json = json.load(fin)\n",
    "\n",
    "print(len(data_json))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "idx_list = list(data_json.keys())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'4399'"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "idx_list[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "data_list = []\n",
    "for i, idx in enumerate(idx_list):\n",
    "    content = data_json[idx][\"content\"]\n",
    "    fact = content[\"本院查明\"]\n",
    "    data_item = {\n",
    "        \"index\": idx,\n",
    "        \"fact\": ''.join(fact),\n",
    "        \"idx\": i\n",
    "    }\n",
    "    \n",
    "    data_list.append(data_item)\n",
    "\n",
    "dataset = Dataset.from_list(data_list)\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "event_type_data_dict = {\n",
    "    \"保证担保\": [\"保证协议\", \"保证方式\", \"保证担保\", \"保证合同\", \"连带保证责任\", \"保证书\", \"连带责任担保\", \"保证人\", \"担保合同\", \"担保函\", \"担保协议\", \"担保书\", \"担保人\", \"保证函\"],\n",
    "    \"借款事件\": [\"借条\", \"借贷关系\", \"借款合同\", \"借款\", \"转账\"],\n",
    "    \"质押担保\": [\"质押\", \"质权人\", \"出质人\"],\n",
    "    \"抵押担保\": [\"抵押担保\", \"抵押协议\", \"抵押人\", \"抵押权人\", \"抵押合同\", \"抵押\"],\n",
    "    \"债权转让\": [\"债权转让\", \"债务转让\", \"转让协议\", \"转移协议\"]\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "b240e93e63f94d7088812575625e7faa",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Map:   0%|          | 0/4024 [00:00<?, ? examples/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "pred_label = []\n",
    "miss_cnt = 0\n",
    "miss_idx_list = []\n",
    "type_cnt_dict = {x:0 for x in event_type_data_dict.keys()}\n",
    "\n",
    "def annonate_event_type(data):\n",
    "    facts = data[\"fact\"]\n",
    "    event_list = []\n",
    "    for event_type, key_list in event_type_data_dict.items():\n",
    "        for k in key_list:\n",
    "            if k in facts:\n",
    "                event_list.append(event_type)\n",
    "                type_cnt_dict[event_type] += 1\n",
    "                break\n",
    "    \n",
    "    if not event_list:\n",
    "        if \"利息\" in facts:\n",
    "            event_list.append(\"借款事件\")\n",
    "            type_cnt_dict[\"借款事件\"] += 1\n",
    "        else:\n",
    "            miss_idx_list.append(data[\"idx\"])\n",
    "    pred_label.append(';'.join(event_list))\n",
    "    data[\"event_type\"] = event_list\n",
    "    idx_list.append(data[\"index\"])\n",
    "    return data\n",
    "\n",
    "annotated_dataset = dataset.map(annonate_event_type)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'保证担保': 674, '借款事件': 3934, '质押担保': 81, '抵押担保': 605, '债权转让': 288}"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type_cnt_dict"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "6276\n"
     ]
    }
   ],
   "source": [
    "print(dataset[miss_idx_list[8]][\"index\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'index': '1661',\n",
       " 'fact': '【一审法院查明】一审法院认定事实：2018年10月25日和11月3日，汤伟军、秦徂强签订借款合同两份，载明秦徂强向汤伟军借款30万元整和100万元整，借款用途为生意周转，借款期限为6个月，借款利息为3%按月结息。并约定秦徂强以坐落于XX路XX弄XX号XX的房屋为本合同借款提供抵押担保。合同文末均由汤伟军、秦徂强签字并具名日期。双方并未实际就上海市XX路XX弄XX号XX室房屋办理抵押登记。2018年10月25日，汤伟军通过银行转账方式向秦徂强支付285,000元，2018年11月3日，汤伟军同样通过银行转账方式向秦徂强支付950,000元，2018年11月16日，汤伟军再次通过银行转账方式向秦徂强支付20,000元。2018年11月25日，秦徂强通过微信向汤伟军支付19,900元；2018年11月27日，秦徂强通过微信向汤伟军支付15,100元；2018年12月23日，秦徂强通过微信向汤伟军支付10,000元；2019年2月28日，秦徂强通过微信向汤伟军支付20,000元；2019年4月20日，秦徂强通过银行转账向汤伟军支付50,000元；2019年5月6日，秦徂强通过微信向汤伟军支付3,000元；2019年5月24日，秦徂强通过微信向汤伟军支付5,500元；2019年6月21日，秦徂强通过微信向汤伟军支付10,000元；2019年6月23日，秦徂强通过微信向汤伟军支付10,000元。一审法院另查明，汤伟军为追讨债务聘请律师，花费律师费5,000元。【本院查明】本院二审期间，当事人未提交新证据。本院经审理查明，一审法院认定事实无误，予以确认。本院另查明，2019年11月20日一审法院庭审中，秦徂强当庭表示：“确实向原告借款，借到125.5万元，事后陆续归还的情况与原告诉状一致，没有其他还款情况，愿意归还借款……同意支付律师费”。',\n",
       " 'idx': 4,\n",
       " 'event_type': ['借款事件', '抵押担保']}"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "annotated_dataset[4]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4024"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(annotated_dataset)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "885e145291704df18040a40d317a4feb",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Saving the dataset (0/1 shards):   0%|          | 0/4024 [00:00<?, ? examples/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "annotated_dataset.save_to_disk(\"muser-dataset\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "chatgpt",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.18"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
